<template>

  <div class="add-edit-position-view" v-loading="loading">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
      <el-form-item :label="$t('tables_title.posts_name')" prop="name">
        <el-input v-model="ruleForm.name" :placeholder="this.$t('tables_title.posts_name')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.posts_code')" prop="code">
        <el-input v-model="ruleForm.code" :placeholder="$t('tables_title.posts_name')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.sort')" prop="sort">
        <el-input type="number" v-model="ruleForm.sort" :placeholder="$t('tables_title.sort')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.remarks')">
        <el-input type="textarea" v-model="ruleForm.remark" :placeholder="$t('tables_title.remarks')"></el-input>
      </el-form-item>

      <div style="text-align: center;">
        <el-button type="primary" @click="submitForm('ruleForm')">{{$t('global.submit')}}</el-button>
        <el-button @click="cancelView">{{$t('global.cancel')}}</el-button>
      </div>
    </el-form>
  </div>

</template>

<script>

import { addPosition,editPosition } from '@/api/positionApi.js'

export default {
  data(){
    return{
      loading:false,
      ruleForm:{
        name:'',
        code:'',
        remark:'',
        sort:''
      },
      rules:{
          name: [
            { required: true, message: this.$t('posts_management.empty_posts_name'), trigger: 'blur' },
          ],
          code: [
            { required: true, message: this.$t('posts_management.empty_posts_code'), trigger: 'blur' },
          ],
          sort: [
            { required: true, message: this.$t('global.empty_sort'), trigger: 'blur' },
          ],
      }
    }
  },
  props: ['editPositionInfo'],
  watch:{
    editPositionInfo(newVal,oldVal){
      this.ruleForm = newVal
    }
  },
  components:{

  },
  created(){
    this.ruleForm = this.editPositionInfo
  },
  methods:{
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.ruleForm.positionId){
            this.userEditPosition()
          }else{
            this.userAddPosition()
          }
        } else {
          return false;
        }
      });
    },
    userEditPosition(){         //修改POSITION信息
      this.loading = true
      editPosition(this.ruleForm).then((res)=>{
        this.loading = false
        console.log(res)
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.modified_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{

      })
    },
    userAddPosition(){          //新增POSITION
      this.loading = true
      addPosition(this.ruleForm).then((res)=>{
        this.loading = false
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.added_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{

      })
    },
    cancelView(){       //关闭弹窗
      this.$emit('cancelView')
    },
    refreshData(){
      this.$emit('refreshData')
    }
  }
}
</script>

<style lang="scss" scoped>
.add-edit-position-view{
  width: 100%;
  box-sizing: border-box;
  padding: 0 30px;
}
</style>
